//
//  PhoneRegisterViewController.m
//  Growth
//
//  Created by 洪波 陈 on 6/9/12.
//  Copyright (c) 2012 MEETStudio. All rights reserved.
//

#import "PhoneRegisterViewController.h"

#import "PhoneLoginViewController.h"
#import "PhoneVerifyViewController.h"

#import "RegexKitLite.h"
#import "WebRequest.h"

static WebConnectionArray *verifyAccount_connection;

@interface PhoneRegisterViewController ()

@end

@implementation PhoneRegisterViewController

@synthesize phoneNumField;

- (void)dealloc
{
    if (verifyAccount_connection != nil) {
        verifyAccount_connection.connectionDelegate = nil;
        [verifyAccount_connection release];
        verifyAccount_connection = nil;
    }
    
    [super dealloc];
    
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    self.navigationItem.title = @"手机注册";
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}


#pragma mark - Web Connection delegate
- (void)connectionArraySuccess:(NSDictionary *)dict  connectionURL:(NSString *)url
{
    CLog(@"dict :%@", dict);
    NSRange range = [url rangeOfString:@"verify/account"]; 
    
    if (range.length > 0)  {
        
        CLog(@"verify phone %@",[dict objectForKey:@"object"] );
        
        if ([[dict objectForKey:@"object"] boolValue]) { 
            
            [self toPhoneVerifyView:nil];
        }
        else{
            [self toPhoneLoginView:nil];
        }
        
    }
    
}

- (void)connectionArrayError:(NSError *)error
{
    [LoadingView showDurationMessageLoadingViewWith:DEFAULT_LOADING_TIME message:@"网络错误，请重试！"];
}

#pragma mark - Action
- (IBAction)nextStepAction:(id)sender
{
    [phoneNumField resignFirstResponder];
    
    NSString *phoneNum = phoneNumField.text;
    BOOL isphone = [phoneNum isMatchedByRegex:REGEX_PHONE];
    CLog(@" Post invite phone!  ************ %d", isphone);
    
    if (isphone) {
        if (NETWORK_STATUS_LOADING) {
            NSMutableURLRequest * request = [WebRequest verifyEmailRequest:phoneNum];
            CLog(@"request %@", request);
            if (verifyAccount_connection != nil) {
                verifyAccount_connection.connectionDelegate = nil;
            }
            verifyAccount_connection = [[WebConnectionArray alloc] initWith:request delegate:self dataBaseListName:nil];
        }
        
    }
    else
    {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"输入有误" message:@"请确认您输入的手机号正确！" 
                                                       delegate:self 
                                              cancelButtonTitle:@"确定" otherButtonTitles:nil];
        
        [alert show];
        [alert release];
    }
    
   
}

- (void)toPhoneLoginView:(id)sender
{
    NSString *phoneNum = phoneNumField.text;
    PhoneLoginViewController *content = [[PhoneLoginViewController alloc] initWithPhone:phoneNum];
    [self.navigationController pushViewController:content animated:YES];
    [content release];
}

- (void)toPhoneVerifyView:(id)sender
{
    NSString *phoneNum = phoneNumField.text;
    PhoneVerifyViewController *content = [[PhoneVerifyViewController alloc] initWithPhone:phoneNum];
    [self.navigationController pushViewController:content animated:YES];
    [content release];
}

@end
